import time
import json
import logging
from paho.mqtt import publish
from ztp_backend.settings import MQTT_HOST, MQTT_PORT


client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
logger = logging.getLogger(__name__)


def push_message(channel, body, level, username, title):
    mqtt_data = {
        "username": username,
        "title": title,
        "body": body,
        "from": "系统通知",
        "level": level,  # 1 表示有高危风险
        "type": 1,
        "deal": 1,
        "warning": ""}
    publish.single(channel, json.dumps(mqtt_data).encode('utf-8').decode("unicode_escape"),
                   client_id=client_id, hostname=MQTT_HOST, port=MQTT_PORT)
    logger.info('成功推送消息到 %s , 消息体为 %s' % (channel, json.dumps(mqtt_data)))



if __name__ == "__main__":
    print("------")
    channel = '822FC9C2119DE9E5B68DD34E93347C92'
    body = '测试消息'
    level = 0
    title = '测试消息'
    push_message(channel, body=body, level=level, username='user_name', title=title)
    print("-------")